<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSave">确定</el-button>
      <el-button @click="$emit('close')">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getRoles } from '@/api/setting.js'
import { getUserDetailById } from '@/api/user.js'
import { assignRoles } from '@/api/employees.js'

export default {
  props: {
    curEmployeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 获取个人信息
    async loadRoles() {
      const { data: res } = await getRoles({ page: 1, pagesize: 1000 })
      // console.log(res)
      this.list = res.rows
      const { data } = await getUserDetailById(this.curEmployeId)
      // console.log(data)
      this.roleIds = data.roleIds
    },
    // 点击确认
    async hSave() {
      await assignRoles({ id: this.curEmployeId, roleIds: this.roleIds })
      this.$emit('close')
      this.$message.success('分配角色成功')
    }
  }
}
</script>
